(function(window){
	'use strict';
	var host = 'https://interface.ultimavip.cn';
	// var host = 'http://testabc.ultimablack.cn';
	function _app() {
		this.url = {
			liang: host + '/card/liang',
      self: host + '/card/random',
      search: host + '/card/search',
      pay: host + '/pay/card/order',
      delivery: host + '/pay/card/order/delivery',
      qrCode: host + '/pay/weixin/qrcode',
      status: host + '/card/status',
      checkImgCode: host + '/pay/validateImgCheckCode',
      sendCode: host + '/pay/smsCheckCode',
      checkSmsCode: host + '/pay/validateSmsCheckCode',
      all: host + '/card/randomAndLiang2',
      weixin: host + '/pay/card',
      union: host + '/pay/unionPc'
		};
		this.isMakeInit = true;
		this.swiper = null;
		this.initial = true;
    this.pageNum = 1; // 搜索分页
		this.source = _getUrlParam('source');
		this.cardType = 'zz'; // 卡面类型  zz 至尊 zs 郑爽 jd 经典 xz 星座
		this.chooseType = -1; // 选号方式 0 自选号码 1 尊享号码 2 随机
		this.isMaken = null; // 是否定制 0 不定制 1 定制
		this.payType = 1; // 支付方式 0 货到付款 1 在线支付
		this.onlinePayType = -1; // 在线支付方式 0 支付宝 1 微信 2 银联
		this.init();
	}

	function _getUrlParam(name) {
		var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)');
		var matched = window.location.search.substr(1).match(reg);
    if (matched != null) {
      return matched[2];
    }
    return null;
	}

	function _bind(fn, context) {
		return function() {
			var args = [].slice.call(arguments);
			fn.apply(context, args);
		}
	}

  function _preload(data) {
      if(!data) return;
      for(var key in data) {
        data[key].forEach(function(src) {
          var imgObj = new Image();
          imgObj.src = src;
        });
      }
  }

	_app.prototype = {
		constructor: _app,
		init: function() {
			var singleTpl = $('#J_single_tpl').html();
			$('#J_main').html(singleTpl);
			this.swiper = new Swiper('.swiper-container', {
				initialSlide: 1,
				slidesPerView: 3,
				slideToClickedSlide: true,
				centeredSlides: true,
				loop: false,
				pagination : '.swiper-pagination',
				paginationClickable: true
			});
			this.initArea();
			this.bindEvent();
		},
		selectCard: function(e) {
			var $target = $(e.target), 
					cardType = $target.data('type');
			this.cardType = cardType;
      $('#J_bar_card').next().text('终身免年费');
      $('#top_select .m-number-item').each(function() {
        var price = $(this).data('price');
        $(this).find('span.fs14').text('（¥'+ price +'）')
      });
			switch(cardType) {
        case 'zt':
          $('#J_sx').hide();
          $('.J_select_card').removeClass('m-btn-active');
          $('#J_constellation').hide();
          $('#J_zs_tip').hide();
          $('#J_bar_card').text($target.text()).next().text('免基础卡费');
          $('#top_select .m-number-item').each(function() {
            var price = $(this).data('price');
            $(this).find('span.fs14').text('（¥'+ (price+2800) +'）')
          });
          break;
        case 'zc':
          $('#J_sx').hide();
          $('.J_select_card').removeClass('m-btn-active');
          $('#J_constellation').hide();
          $('#J_zs_tip').hide();
          $('#J_bar_card').text($target.text()).next().text('免基础卡费');
          $('#top_select .m-number-item').each(function() {
            var price = $(this).data('price');
            $(this).find('span.fs14').text('（¥'+ (price+100) +'）')
          })
          break;
        case 'ns':
          $('#J_sx').hide();
          $('.J_select_card').removeClass('m-btn-active');
          $('#J_constellation').hide();
          $('#J_zs_tip').hide();
          $('#J_bar_card').text($target.text());
          break;
				case 'zz':
          $('#J_sx').hide();
					$('.J_select_card').removeClass('m-btn-active');
					$('#J_constellation').hide();
					$('#J_zs_tip').hide();
					$('#J_bar_card').text($target.text());
					break;
				case 'zs':
          $('#J_sx').hide();
					$('.J_select_card').removeClass('m-btn-active');
					$('#J_constellation').hide();
					$('#J_zs_tip').show();
					$('#J_bar_card').text($target.text());
					break;
				case 'jd':
          $('#J_sx').hide();
					$('.J_select_card').removeClass('m-btn-active');
					$('#J_constellation').hide();
					$('#J_zs_tip').hide();
					$('#J_bar_card').text($target.text());
					break;
				case 'xz': 
          $('#J_sx').hide();
          $('.J_select_card').removeClass('m-btn-active');
          $('#J_constellation').show();
          $('#J_sx').hide();
          $('#J_zs_tip').hide();
          $('.m-constellation-btns .J_select_card').eq(0).click();
          break;
        case 'sx':
          $('.J_select_card').removeClass('m-btn-active');
          $('#J_constellation').hide();
          $('#J_sx').show();
          $('#J_zs_tip').hide();
          $('.m-sx-btns .J_select_card').eq(0).click();
          break;
				default: 
			    if($('#J_constellation').is(':visible')) { // 点击星座卡
            $target.siblings().removeClass('m-btn-active');
            $('#J_bar_card').text($target.text() + '座');
          } 
          else if($('#J_sx').is(':visible')) {
            $target.siblings().removeClass('m-btn-active');
            $('#J_bar_card').text('十二生肖卡（' + $target.text() + '）');
          }
			}
      this.renderBarPrice();
      this.toggleDelivery(cardType);
			this.changeGallery(cardType);
			$target.addClass('m-btn-active');
		},
    toggleDelivery: function(cardType) {
      if(cardType == 'zt' || cardType == 'zc') {
        $('.J_pay_btn[data-type="0"]').hide();
      } else {
        $('.J_pay_btn[data-type="0"]').show();
      }
    },
    scroll: function() {
      var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      var top = $('#J_edit').offset().top;
      if(scrollTop > 0 ) {
        $('.m-bar-wrapper').addClass('m-bar-wrapper-fixed');
        $('.header').hide();
      } else {
        $('.header').show();
        $('.m-bar-wrapper').removeClass('m-bar-wrapper-fixed');
      }
    }, 
		changeGallery: function(cardType) {
			var arr = {
        'zc': ['https://img1.ultimavip.cn/2018/pc-zc-1.png', 'https://img1.ultimavip.cn/2018/pc-zc-2.png', 'https://img1.ultimavip.cn/2018/pc-zc-3.png'],
        'zt': ['https://img1.ultimavip.cn/2018/pc-zt-1.png', 'https://img1.ultimavip.cn/2018/pc-zt-2.png', 'https://img1.ultimavip.cn/2018/pc-zt-3.png'],
        'ns': ['https://img1.ultimavip.cn/2018/pc-ns-1.png', 'https://img1.ultimavip.cn/2018/pc-ns-2.png', 'https://img1.ultimavip.cn/2018/pc-ns-3.png'],
				'zz': ['https://img1.ultimavip.cn/2018/pc-zz-1.png', 'https://img1.ultimavip.cn/2018/pc-zz-2-1.png', 'https://img1.ultimavip.cn/2018/pc-zz-3.png'],
				'zs': ['https://img1.ultimavip.cn/2018/pc-zs-1.png', 'https://img1.ultimavip.cn/2018/pc-zs-2-1.png', 'https://img1.ultimavip.cn/2018/pc-zs-3.png'],
				'jd': ['https://img1.ultimavip.cn/2018/pc-jd-1.png', 'https://img1.ultimavip.cn/2018/pc-jd-2-1.png', 'https://img1.ultimavip.cn/2018/pc-jd-3.png'],
				'by': ['https://img1.ultimavip.cn/2018/pc-by-1.png', 'https://img1.ultimavip.cn/2018/pc-by-2-2.png', 'https://img1.ultimavip.cn/2018/pc-by-3.png'],
				'cn': ['https://img1.ultimavip.cn/2018/pc-cn-1.png', 'https://img1.ultimavip.cn/2018/pc-cn-2-1.png', 'https://img1.ultimavip.cn/2018/pc-cn-3.png'],
				'jn': ['https://img1.ultimavip.cn/2018/pc-jn-1.png', 'https://img1.ultimavip.cn/2018/pc-jn-2-1.png', 'https://img1.ultimavip.cn/2018/pc-jn-3.png'],
				'jx': ['https://img1.ultimavip.cn/2018/pc-jx-1.png', 'https://img1.ultimavip.cn/2018/pc-jx-2-1.png', 'https://img1.ultimavip.cn/2018/pc-jx-3.png'],
				'mj': ['https://img1.ultimavip.cn/2018/pc-mj-1.png', 'https://img1.ultimavip.cn/2018/pc-mj-2-1.png', 'https://img1.ultimavip.cn/2018/pc-mj-3.png'],
				'ss': ['https://img1.ultimavip.cn/2018/pc-ss-1.png', 'https://img1.ultimavip.cn/2018/pc-ss-2-1.png', 'https://img1.ultimavip.cn/2018/pc-ss-3.png'],
				'shizi': ['https://img1.ultimavip.cn/2018/pc-shizi-1.png', 'https://img1.ultimavip.cn/2018/pc-shizi-2-1.png', 'https://img1.ultimavip.cn/2018/pc-shizi-3.png'],
				'sy': ['https://img1.ultimavip.cn/2018/pc-sy-1.png', 'https://img1.ultimavip.cn/2018/pc-sy-2-1.png', 'https://img1.ultimavip.cn/2018/pc-sy-3.png'],
				'sz': ['https://img1.ultimavip.cn/2018/pc-sz-1.png', 'https://img1.ultimavip.cn/2018/pc-sz-2-1.png', 'https://img1.ultimavip.cn/2018/pc-sz-3.png'],
				'sp': ['https://img1.ultimavip.cn/2018/pc-sp-1.png', 'https://img1.ultimavip.cn/2018/pc-sp-2-1.png', 'https://img1.ultimavip.cn/2018/pc-sp-3.png'],
				'tc': ['https://img1.ultimavip.cn/2018/pc-tc-1.png', 'https://img1.ultimavip.cn/2018/pc-tc-2-1.png', 'https://img1.ultimavip.cn/2018/pc-tc-3.png'],
				'tx': ['https://img1.ultimavip.cn/2018/pc-tx-1.png', 'https://img1.ultimavip.cn/2018/pc-tx-2-1.png', 'https://img1.ultimavip.cn/2018/pc-tx-3.png'],
			  'mouse': ['https://img1.ultimavip.cn/2018/mouse-1.png', 'https://img1.ultimavip.cn/2018/mouse-2.png', 'https://img1.ultimavip.cn/2018/mouse-3.png'],
        'cattle': ['https://img1.ultimavip.cn/2018/cattle-1.png', 'https://img1.ultimavip.cn/2018/cattle-2.png', 'https://img1.ultimavip.cn/2018/cattle-3.png'],
        'tiger': ['https://img1.ultimavip.cn/2018/tiger-1.png', 'https://img1.ultimavip.cn/2018/tiger-2.png', 'https://img1.ultimavip.cn/2018/tiger-3.png'],
        'rabbit': ['https://img1.ultimavip.cn/2018/rabbit-1.png', 'https://img1.ultimavip.cn/2018/rabbit-2.png', 'https://img1.ultimavip.cn/2018/rabbit-3.png'],
        'dragon': ['https://img1.ultimavip.cn/2018/dragon-1.png', 'https://img1.ultimavip.cn/2018/dragon-2.png', 'https://img1.ultimavip.cn/2018/dragon-3.png'],
        'snake': ['https://img1.ultimavip.cn/2018/snake-1.png', 'https://img1.ultimavip.cn/2018/snake-2.png', 'https://img1.ultimavip.cn/2018/snake-3.png'],
        'horse': ['https://img1.ultimavip.cn/2018/horse-1.png', 'https://img1.ultimavip.cn/2018/horse-2.png', 'https://img1.ultimavip.cn/2018/horse-3.png'],
        'sheep': ['https://img1.ultimavip.cn/2018/sheep-1.png', 'https://img1.ultimavip.cn/2018/sheep-2.png', 'https://img1.ultimavip.cn/2018/sheep-3.png'],
        'monkey': ['https://img1.ultimavip.cn/2018/monkey-1.png', 'https://img1.ultimavip.cn/2018/monkey-2.png', 'https://img1.ultimavip.cn/2018/monkey-3.png'],
        'rooster': ['https://img1.ultimavip.cn/2018/rooster-1.png', 'https://img1.ultimavip.cn/2018/rooster-2.png', 'https://img1.ultimavip.cn/2018/rooster-3.png'],
        'dog': ['https://img1.ultimavip.cn/2018/dog-1.png', 'https://img1.ultimavip.cn/2018/dog-2.png', 'https://img1.ultimavip.cn/2018/dog-3.png'],
        'pig': ['https://img1.ultimavip.cn/2018/pig-1.png', 'https://img1.ultimavip.cn/2018/pig-2.png', 'https://img1.ultimavip.cn/2018/pig-3.png']
      
      };
			if(!arr[cardType]) return;
			for(var i = 0; i < arr[cardType].length; i++) {
				$('.swiper-slide').eq(i).find('img').attr('src', arr[cardType][i]);
			}
      this.swiper.slideTo(1);
		},
		getListForHtml: function(list) {
			var str = '', numStr, self = this;
      $.each(list, function(index, item) {
          var price = item['price'];
          if(self.cardType == 'zc') {
              price += 100;
          } else if(self.cardType == 'zt') {
              price += 2800;
          }
          switch (item['type']) {
              case 0:
                  str += '<div class="m-number-item" data-id="' + item.num + '">' + item['num'] + '</div>';
                  break;
              case 1:
                  if(/(\d)(\1{2,})/g.test(item['num'])) {
                    numStr = item['num'].replace(/(\d)(\1{2,})/g, '<span style="color: #DDA960;font-weight: bold">$1$2</span>')
                  } else {
                    numStr = item['num'];
                  }
                  str += '<div class="m-number-item" data-id="' + item.num + '" data-price="' + item.price + '">' + numStr + '<span class="fs12 c0">（¥' + price + '）</span></div>';
                  break;
          }
      });
      return str;
		},
		renderOwnNumber: function() {
			var self = this;
			// $('#self_select').html('<div class="loading">正在加载...</div>');
      $.get(this.url.self, { pmtype: 2 }, function(response) {
          if (response['success']) {
              var listStr = self.getListForHtml(response['data']);
              $('#self_select').html(listStr);
          } else {
              console.log(response['msg'])
          }
      });
		},
		renderTopNumber: function() {
			var self = this;
			// $('#top_select').html('<div class="loading">正在加载...</div>');
      $.get(this.url.liang, { pmtype: 2 }, function(response) {
          if (response['success']) {
              var listStr = self.getListForHtml(response['data']);
              $('#top_select').html(listStr);
          } else {
              console.log(response['msg'])
          }
      });
		},
		renderNumber: function() {
			var self = this;
			$.get(this.url.all, { pmtype: 2 }, function(response) {
          if (response['success']) {
              var self_listStr = self.getListForHtml(response['data']['commonCardList']);
              $('#self_select').html(self_listStr);
              var top_listStr = self.getListForHtml(response['data']['liangCardList']);
              $('#top_select').html(top_listStr);
              self.initial = false;
          } else {
              console.log(response['msg'])
          }
      });
		},
    getSearchHtml: function(cardNum, list) {
      var str = '';
      var self = this;
      $.each(list, function(index, item) {
          var numStr = item['num'].replace(new RegExp('(' + cardNum + ')'), '<span style="color: #DDA960;font-weight: bold">$1</span>');
          var price = item['price'];
          if(self.cardType == 'zc') {
              price += 100;
          } else if(self.cardType == 'zt') {
              price += 2800;
          }
          switch (item['type']) {
              case 0:
                  str += '<div class="m-number-item" data-id="' + item.num + '">' + numStr + '</div>';
                  break;
              case 1:
                  str += '<div class="m-number-item" data-id="' + item.num + '" data-price="' + item.price + '">' + numStr + '<span class="fs14 c0">（¥' + price + '）</span></div>';
                  break;
          }
      });
      return str;
    },
		renderSearchNumber: function(cardNum, callback) {
			var self = this;
      $('#J_search_result').show();
      // $('#search_select').html('<div class="loading">正在加载...</div>');
      $.get(this.url.search, {
          pageNum: this.pageNum,
          cardNum: cardNum
      }, function(response) {
          if (response['success']) {
              if (response['data']) {
                  var listStr = self.getSearchHtml(cardNum, response['data']['list']);
                  $('#search_select').html(listStr);
                  if (response['data']['nextPage'] > 0) {
                    self.pageNum = response['data']['nextPage'];
                  } else {
                  	self.pageNum = 1;
                  }
              } else {
                  $('#J_search_result').show();
                  $('#search_select').html('<div class="tc fs20" style="padding: 50px 0 0">暂无数据</div>');
              }
          } else {
              alert(response['msg'])
          }
          callback && callback();
      })
		},
		switchSearch: function() {
			 var cardNum = $.trim($('#J_search').find('input').val());
      if(!cardNum) return alert('请输入卡号！');
      this.renderSearchNumber(cardNum, function() {
          $this.removeClass('disabled');
      });
		},
		search: function(e) {
        var $this = $(e.target);
        var cardNum = $.trim($(e.target).prev().val());
        if(!cardNum) return alert('请输入卡号！');
        if ($this.hasClass('disabled')) return;
        this.pageNum = 1;
        $this.addClass('disabled');
        this.renderSearchNumber(cardNum, function() {
            $this.removeClass('disabled');
        });
    },
    selectNumberType: function(e) {
			var $target = $(e.target), 
					type = $target.data('type');
			$('.J_select_number').removeClass('m-btn-active');
			$target.addClass('m-btn-active');
			switch(type) {
				case 'xh':
					$('#J_number_box').show();
					$('#J_search').show();
          $('#J_self').hide();
					// this.renderNumber();
					this.chooseType = -1;
					$('#J_bar_number').text('自选号码');
					$('#J_bar_number').parent().show();
					break;
				case 'sj':
					$('.m-number-item').removeClass('active');
					$('#J_number_box').hide();
					$('#J_search').hide();
          $('#J_self').show();
					$('[name="cardPrice"]').val('');
          $('[name="cardNum"]').val('');
          $('#J_bar_number').text('随机分配');
          $('#J_bar_number').parent().show();
          this.chooseType = 2;
          this.renderBarPrice();
					break;
			}
		},
    selectNumber: function(e) {
			var cardNum = $(e.currentTarget).data('id');
      var price = $(e.currentTarget).data('price') || 20;
      $('[name="cardPrice"]').val(price);
      $('[name="cardNum"]').val(cardNum);
      $('.m-number-item').removeClass('active');
      $(e.currentTarget).addClass('active');
      if(price !== 20) {
      	this.chooseType = 1;
      } else {
      	this.chooseType = 0;
      }
      this.renderBarPrice();
		},
		initArea: function() {
    	var options = this.getProvince();
    	$('[name="province"]').html(options);
    },
		getProvince: function() {
        var html = "<option value=''>请选择省份</option>";
        for (var i = 0; i < PROVINCE.length; i++) {
            html += '<option value="' + PROVINCE[i][0] + '">' + PROVINCE[i][1] + '</option>';
        }
        return html;
    },
    getCity: function(provinceCode) {
        var html = "<option value=''>请选择城市</option>";
        var cityArr = [];
        for (var i = 0; i < CITY.length; i++) {
            if (CITY[i][1] == provinceCode) {
                cityArr.push(CITY[i]);
            }
        }
        for (var i = 0; i < cityArr.length; i++) {
            html += '<option value="' + cityArr[i][0] + '">' + cityArr[i][2] + '</option>';
        }
        return html;
    },
    getCounty: function(cityCode) {
        var html = "<option value=''>请选择地区</option>";
        var countyArr = [];
        for (var i = 0; i < COUNTY.length; i++) {
            if (COUNTY[i][1] == cityCode) {
                countyArr.push(COUNTY[i]);
            }
        }
        if (countyArr.length > 0) {
            for (var i = 0; i < countyArr.length; i++) {
                html += '<option value="' + countyArr[i][0] + '">' + countyArr[i][2] + '</option>';
            }
        }
        return html;
    },
    selectProvince: function() {
    	var code = $('[name="province"]').val();
    	var options = this.getCity(code);
    	$('[name="city"]').html(options);
    	$('[name="county"]').html('<option value="">请选择地区</option>');
    	$('[name="province"]').closest('.select-group').removeClass('select-group-error');
    },
    selectCity: function() {
			var code = $('[name="city"]').val();
    	var options = this.getCounty(code);
    	$('[name="county"]').html(options);
    	$('[name="city"]').closest('.select-group').removeClass('select-group-error');
    },
    renderOne: function() {
    	var typeStr, imgStr;
    	var arr = {
        'zc': 'https://img1.ultimavip.cn/pc/pc-zc.png',
        'zt': 'https://img1.ultimavip.cn/pc/pc-zt.png',
        'ns': 'https://img1.ultimavip.cn/pc/pc-ns.png',
				'zz': 'https://img1.ultimavip.cn/2018/pc-zz-2-1.png',
				'zs': 'https://img1.ultimavip.cn/2018/pc-zs-2-1.png',
				'jd': 'https://img1.ultimavip.cn/2018/pc-jd-2-1.png',
				'by': 'https://img1.ultimavip.cn/2018/pc-by-2-2.png',
				'cn': 'https://img1.ultimavip.cn/2018/pc-cn-2-1.png',
				'jn': 'https://img1.ultimavip.cn/2018/pc-jn-2-1.png',
				'jx': 'https://img1.ultimavip.cn/2018/pc-jx-2-1.png',
				'mj': 'https://img1.ultimavip.cn/2018/pc-mj-2-1.png',
				'ss': 'https://img1.ultimavip.cn/2018/pc-ss-2-1.png',
				'shizi': 'https://img1.ultimavip.cn/2018/pc-shizi-2-1.png',
				'sy': 'https://img1.ultimavip.cn/2018/pc-sy-2-1.png', 
				'sz': 'https://img1.ultimavip.cn/2018/pc-sz-2-1.png', 
				'sp': 'https://img1.ultimavip.cn/2018/pc-sp-2-1.png', 
				'tc': 'https://img1.ultimavip.cn/2018/pc-tc-2-1.png',
				'tx': 'https://img1.ultimavip.cn/2018/pc-tx-2-1.png',
        'mouse': 'https://img1.ultimavip.cn/pc/pc-mouse.png',
        'cattle': 'https://img1.ultimavip.cn/pc/pc-cattle.png',
        'tiger': 'https://img1.ultimavip.cn/pc/pc-tiger.png',
        'rabbit': 'https://img1.ultimavip.cn/pc/pc-rabbit.png',
        'dragon': 'https://img1.ultimavip.cn/pc/pc-dragon.png',
        'snake': 'https://img1.ultimavip.cn/pc/pc-snake.png',
        'horse': 'https://img1.ultimavip.cn/pc/pc-horse.png',
        'sheep': 'https://img1.ultimavip.cn/pc/pc-sheep.png',
        'monkey': 'https://img1.ultimavip.cn/pc/pc-monkey.png',
        'rooster': 'https://img1.ultimavip.cn/pc/pc-rooster.png',
        'dog': 'https://img1.ultimavip.cn/pc/pc-dog.png',
        'pig': 'https://img1.ultimavip.cn/pc/pc-pig.png'
			};
			imgStr = arr[this.cardType];
    	switch(this.cardType) {
          case 'zc':
            typeStr = '总裁会籍';
            break;
          case 'zt':
            typeStr = '总统会籍';
            break;
        	case 'zz':
        		typeStr = '至尊会籍';
        		break;
        	case 'zs':
        		typeStr = '郑爽联名卡';
        		break;
        	case 'jd':
        		typeStr = '经典会籍';
        		break;
        	case 'by':
        		typeStr = '白羊座';
        		break;
        	case 'jn':
        		typeStr = '金牛座';
        		break;
        	case 'sz':
        		typeStr = '双子座';
        		break;
        	case 'jx':
        		typeStr = '巨蟹座';
        		break;
        	case 'shizi':
        		typeStr = '狮子座';
        		break;
        	case 'cn':
        		typeStr = '处女座';
        		break;
        	case 'tc':
        		typeStr = '天秤座';
        		break;
        	case 'tx':
        		typeStr = '天蝎座';
        		break;
        	case 'ss':
        		typeStr = '射手座';
        		break;
        	case 'mj':
        		typeStr = '摩羯座';
        		break;
        	case 'sp':
        		typeStr = '水瓶座';
        		break;
        	case 'sy':
        		typeStr = '双鱼座';
        		break;
          case 'mouse':
            typeStr = '鼠';
            break;
          case 'cattle':
            typeStr = '牛';
            break;
          case 'tiger':
            typeStr = '虎';
            break;
          case 'rabbit':
            typeStr = '兔';
            break;
          case 'dragon':
            typeStr = '龙';
            break;
          case 'snake':
            typeStr = '蛇';
            break;
          case 'horse':
            typeStr = '马';
            break;
          case 'sheep':
            typeStr = '羊';
            break;
          case 'monkey':
            typeStr = '猴';
            break;
          case 'rooster':
            typeStr = '鸡';
            break;
          case 'dog':
            typeStr = '狗';
            break;
          case 'pig':
            typeStr = '猪';
            break;
          case 'ns':
            typeStr = '女神卡';
            break;
      }
      $('.m-complete-card img', '#J_step_1').attr('src', imgStr);
      $('.m-complete-info .fs14', '#J_step_1').text(typeStr);
    },
    renderTwo: function(name, phone, address) {
    	$('#J_text_name').text(name);
    	$('#J_text_phone').text(phone);
    	$('#J_text_address').text(address);
    },
    renderThree: function() {
    	if(this.chooseType == 0 || this.chooseType == 1) {
    		var cardNum = $('[name="cardNum"]').val();
    		$('#J_text_number').text(cardNum);
    	}
    	if(this.chooseType == 2) {
    		$('#J_text_number').text('随机分配');
    	}
    },
    renderFour: function() {
    	if(this.isMaken === 1) {
    		var firstName = $.trim($('[name="firstName"]').val());
        var lastName = $.trim($('[name="lastName"]').val());
        $('#J_text_make').text(firstName + ' ' + lastName);
    	} else {
    		$('#J_text_make').text('放弃定制')
    	}
    },
    toPrev: function() {
    	$('#J_edit').show();
    	$('#J_confirm').hide();
      this.scroll();
      $(window).on('scroll', _bind(this.scroll, this));
    },
    renderCardNumber: function(cardNum) {
			var left = cardNum.substr(0, 4);
			var right = cardNum.substr(4, cardNum.length);
			if(
        this.cardType == 'zz' || 
        this.cardType == 'zs' || 
        this.cardType == 'jd' || 
        this.cardType == 'zc' ||
        this.cardType == 'zt' ||
        this.cardType == 'mouse' ||
        this.cardType == 'cattle' ||
        this.cardType == 'tiger' ||
        this.cardType == 'rabbit' ||
        this.cardType == 'dragon' ||
        this.cardType == 'snake' ||
        this.cardType == 'horse' ||
        this.cardType == 'sheep' ||
        this.cardType == 'monkey' ||
        this.cardType == 'rooster' ||
        this.cardType == 'dog' ||
        this.cardType == 'pig' 
      ) {
				var leftStr = '', rightStr = '';
				for(var i = 0; i < left.length; i++) {
					leftStr += '<span class="number number-'+left.charAt(i)+'"></span>';
				}
				for(var i = 0; i < right.length; i++) {
					rightStr += '<span class="number number-'+right.charAt(i)+'"></span>';
				}		
				$('.card-number-left').html(leftStr);
				$('.card-number-right').html(rightStr);		
			} else {
				var leftStr = '', rightStr = '';
				for(var i = 0; i < left.length; i++) {
					leftStr += '<span class="number number-'+left.charAt(i)+'-xz"></span>';
				}
				for(var i = 0; i < right.length; i++) {
					rightStr += '<span class="number number-'+right.charAt(i)+'-xz"></span>';
				}	
				$('.card-number-left').html(leftStr);
				$('.card-number-right').html(rightStr);
			}
		},
		scrollToNext: function(next) {
			var offset = $('#J_step_'+next).offset();
      $('html, body').animate({'scrollTop': offset.top - 64});
		},
    toNext: function(e) {
    	var step = $(e.target).data('step'),
    			next = step + 1;
			switch(step) {
				case 1:
					if(this.cardType == 'xz') return alert('请选择星座卡！');
          if(this.cardType == 'sx') return alert('请选择十二生肖卡！');
					this.renderOne();
					try {
						_czc.push(["_trackEvent", 'pc购卡页点击（single）', '下一步', '选择卡面页面', 'single']);
					} catch(e) {
						console.log(e);
					}
          try {
            sensors.track('Search_Select_Membership');
          } catch(e) {
            console.log(e);
          }
					break;
				case 2:
		      var name = $.trim($('[name="name"]').val());
		      var phone = $.trim($('[name="phone"]').val());
		      var address = $.trim($('[name="address"]').val());
		      var province = $('[name="province"] option:selected').text();
		      var city = $('[name="city"] option:selected').text();
		      var county = $('[name="county"] option:selected').text();
		      if (!name) {
		      	$('[name="name"]').closest('.form-input').find('.error-tips').text('请填写收件人');
		      	$('[name="name"]').closest('.form-input').addClass('form-input-error');
		      	return;
		      }
		      if (!phone) {
		      	$('[name="phone"]').closest('.form-input').find('.error-tips').text('请填写联系电话');
		      	$('[name="phone"]').closest('.form-input').addClass('form-input-error');
		      	return;
		      } 
		      if (!/^(13[0-9]|14[579]|15[0-3,5-9]|17[0135678]|18[0-9])\d{8}$/.test(phone)) {
		      	$('[name="phone"]').closest('.form-input').find('.error-tips').text('请您填写正确的手机号');
		      	$('[name="phone"]').closest('.form-input').addClass('form-input-error');
		      	return;
		      }
		      if (province === '请选择省份') {
            $('[name="province"]').closest('.select-group').find('.error-tips').css('left', '8px');
		      	$('[name="province"]').closest('.select-group').find('.error-tips').text('请选择省份');
		      	$('[name="province"]').closest('.select-group').addClass('select-group-error');
		      	return;
		      }
		      if (city === '请选择城市') {
            $('[name="city"]').closest('.select-group').find('.error-tips').css('left', '118px');
		      	$('[name="city"]').closest('.select-group').find('.error-tips').text('请选择城市');
		      	$('[name="city"]').closest('.select-group').addClass('select-group-error');
		      	return;
		      }
		      if (county === '请选择地区') {
            $('[name="county"]').closest('.select-group').find('.error-tips').css('left', '238px');
		      	$('[name="county"]').closest('.select-group').find('.error-tips').text('请选择地区');
		      	$('[name="county"]').closest('.select-group').addClass('select-group-error');
		      	return;
		      } 

		      if (!address) {
		      	$('[name="address"]').closest('.form-input').find('.error-tips').text('请填写详细收件地址');
		      	$('[name="address"]').closest('.form-input').addClass('form-input-error');
		      	return;
		      }
		      address = province + ' ' + city + ' ' + county + ' ' + address;
		      this.renderTwo(name, phone, address);
					if(this.initial) {
						$('#J_bar_number').text('自选号码');
						$('#J_bar_number').parent().show();
						this.renderNumber();
					}
					try {
						_czc.push(["_trackEvent", 'pc购卡页点击（single）', '下一步', '填写信息页面', 'single']);
					} catch(e) {
						console.log(e);
					}
          // try {
          //     sensors.track('Search_Select_Cardnumber');
          // } catch(e) {
          //     console.log(e);
          // }
					break;
				case 3:
					if(this.chooseType === -1) return alert('请选择卡号！');
					if(this.isMakeInit) {
						$('.J_select_make[data-type="1"]').click();
					}
					this.renderThree();
					try {
						_czc.push(["_trackEvent", 'pc购卡页点击（single）', '下一步', '选择卡号页面', 'single']);
					} catch(e) {
						console.log(e);
					}
					try {
            sensors.track('Search_Select_Cardnumber');
          } catch(e) {
            console.log(e);
          }
					break;
				case 4:
					var firstName = $.trim($('[name="firstName"]').val());
          var lastName = $.trim($('[name="lastName"]').val());
          if(this.isMaken === 1) {
            if(firstName == '') {
              $('[name="firstName"]').closest('.form-input').find('.error-tips').text('请输入定制的姓氏拼音');
              $('[name="firstName"]').closest('.form-input').addClass('form-input-error');
              return;
            }
            if(!/^(?:[A-Z]\s*)+$/g.test(firstName)) {
              $('[name="firstName"]').closest('.form-input').find('.error-tips').text('定制姓名只能为大写字母，不能出现空格等特殊字符');
              $('[name="firstName"]').closest('.form-input').addClass('form-input-error');
              return;
            }
            if(lastName == '') {
              $('[name="lastName"]').closest('.form-input').find('.error-tips').text('请输入定制的姓名拼音');
              $('[name="lastName"]').closest('.form-input').addClass('form-input-error');
              return;
            }
            if(!/^(?:[A-Z]\s*)+$/g.test(lastName)) {
              $('[name="lastName"]').closest('.form-input').find('.error-tips').text('定制姓名只能为大写字母，不能出现空格等特殊字符');
              $('[name="lastName"]').closest('.form-input').addClass('form-input-error');
              return;
            }
	          if(((firstName.length + lastName.length) > 20)) return alert('定制姓名字符长度不能超过20');
          }
          this.renderFour();
          try {
						_czc.push(["_trackEvent", 'pc购卡页点击（single）', '下一步', '是否定制页面', 'single']);
					} catch(e) {
						console.log(e);
					}
          try {
            sensors.track('Search_Select_Name');
          } catch(e) {
            console.log(e);
          }
					break;
			}
			$('#J_step_' + step).addClass('s-step-complete');
			$('#J_step_' + next).removeClass('s-step-init');
      $('#J_step_' + next).removeClass('s-step-complete');
      switch(step) {
        case 1:
          $('[name="name"]').focus();
          break;
        case 3:
          $('[name="firstName"]').focus();
          break;
      }
			this.scrollToNext(next);
    },
    toConfirm: function() {
    	var typeStr, imgStr;
    	var arr = {
        'zc': 'https://img1.ultimavip.cn/pc/pc-zc.png',
        'zt': 'https://img1.ultimavip.cn/pc/pc-zt.png',
        'ns': 'https://img1.ultimavip.cn/pc/pc-ns.png',
				'zz': 'https://img1.ultimavip.cn/pc/pc-zz-2.png',
				'zs': 'https://img1.ultimavip.cn/pc/pc-zs-2.png',
				'jd': 'https://img1.ultimavip.cn/pc/pc-jd-2.png',
				'by': 'https://img1.ultimavip.cn/pc/pc-by-2.png',
				'cn': 'https://img1.ultimavip.cn/pc/pc-cn-2.png',
				'jn': 'https://img1.ultimavip.cn/pc/pc-jn-2.png',
				'jx': 'https://img1.ultimavip.cn/pc/pc-jx-2.png',
				'mj': 'https://img1.ultimavip.cn/pc/pc-mj-2.png',
				'ss': 'https://img1.ultimavip.cn/pc/pc-ss-2.png',
				'shizi': 'https://img1.ultimavip.cn/pc/pc-shizi-2.png',
				'sy': 'https://img1.ultimavip.cn/pc/pc-sy-2.png', 
				'sz': 'https://img1.ultimavip.cn/pc/pc-sz-2.png', 
				'sp': 'https://img1.ultimavip.cn/pc/pc-sp-2.png', 
				'tc': 'https://img1.ultimavip.cn/pc/pc-tc-2.png',
				'tx': 'https://img1.ultimavip.cn/pc/pc-tx-2.png',
        'mouse': 'https://img1.ultimavip.cn/pc/pc-mouse.png',
        'cattle': 'https://img1.ultimavip.cn/pc/pc-cattle.png',
        'tiger': 'https://img1.ultimavip.cn/pc/pc-tiger.png',
        'rabbit': 'https://img1.ultimavip.cn/pc/pc-rabbit.png',
        'dragon': 'https://img1.ultimavip.cn/pc/pc-dragon.png',
        'snake': 'https://img1.ultimavip.cn/pc/pc-snake.png',
        'horse': 'https://img1.ultimavip.cn/pc/pc-horse.png',
        'sheep': 'https://img1.ultimavip.cn/pc/pc-sheep.png',
        'monkey': 'https://img1.ultimavip.cn/pc/pc-monkey.png',
        'rooster': 'https://img1.ultimavip.cn/pc/pc-rooster.png',
        'dog': 'https://img1.ultimavip.cn/pc/pc-dog.png',
        'pig': 'https://img1.ultimavip.cn/pc/pc-pig.png'
			};
      try {
        _czc.push(["_trackEvent", 'pc购卡页点击（single）', '下一步', '选择支付方式', 'single']);
      } catch(e) {
        console.log(e); 
      }
      try {
        sensors.track('Search_Select_Submitapplication');
      } catch(e) {
        console.log(e);
      }
			switch(this.cardType) {
          case 'zc':
            typeStr = '总裁会籍';
            break;
          case 'zt':
            typeStr = '总统会籍';
            break;
        	case 'zz':
        		typeStr = '至尊会籍';
        		break;
        	case 'zs':
        		typeStr = '郑爽联名卡';
        		break;
        	case 'jd':
        		typeStr = '经典会籍';
        		break;
        	case 'by':
        		typeStr = '白羊座';
        		break;
        	case 'jn':
        		typeStr = '金牛座';
        		break;
        	case 'sz':
        		typeStr = '双子座';
        		break;
        	case 'jx':
        		typeStr = '巨蟹座';
        		break;
        	case 'shizi':
        		typeStr = '狮子座';
        		break;
        	case 'cn':
        		typeStr = '处女座';
        		break;
        	case 'tc':
        		typeStr = '天秤座';
        		break;
        	case 'tx':
        		typeStr = '天蝎座';
        		break;
        	case 'ss':
        		typeStr = '射手座';
        		break;
        	case 'mj':
        		typeStr = '摩羯座';
        		break;
        	case 'sp':
        		typeStr = '水瓶座';
        		break;
        	case 'sy':
        		typeStr = '双鱼座';
        		break;
          case 'mouse':
            typeStr = '鼠';
            break;
          case 'cattle':
            typeStr = '牛';
            break;
          case 'tiger':
            typeStr = '虎';
            break;
          case 'rabbit':
            typeStr = '兔';
            break;
          case 'dragon':
            typeStr = '龙';
            break;
          case 'snake':
            typeStr = '蛇';
            break;
          case 'horse':
            typeStr = '马';
            break;
          case 'sheep':
            typeStr = '羊';
            break;
          case 'monkey':
            typeStr = '猴';
            break;
          case 'rooster':
            typeStr = '鸡';
            break;
          case 'dog':
            typeStr = '狗';
            break;
          case 'pig':
            typeStr = '猪';
            break;
          case 'ns':
            typeStr = '女神卡';
            break;
      }
    	var firstName = $.trim($('[name="firstName"]').val());
      var lastName = $.trim($('[name="lastName"]').val());
    	if(this.cardType == 'xz') return alert('请选择星座卡！');
    	if(this.chooseType === -1) return alert('请选择卡号！');
      if(this.isMaken === 1) {
      	if(( firstName == '' || lastName == '')) return alert('请填写定制信息！');
        if((!/^(?:[A-Z]\s*)+$/g.test(firstName) || !/^(?:[A-Z]\s*)+$/g.test(lastName))) return alert('定制姓名只能为大写字母，不能出现空格等特殊字符');
        if(((firstName.length + lastName.length) > 20)) return alert('定制姓名字符长度不能超过20');
      }
      var cardPrice = $('[name="cardPrice"]').val();
      var cardNum = $('[name="cardNum"]').val();
      var name = $.trim($('[name="name"]').val());
      var phone = $.trim($('[name="phone"]').val());
      var address = $.trim($('[name="address"]').val());
      var province = $('[name="province"] option:selected').text();
      var city = $('[name="city"] option:selected').text();
      var county = $('[name="county"] option:selected').text();
      var firstName = $.trim($('[name="firstName"]').val());
      var lastName = $.trim($('[name="lastName"]').val());
      if (!name) return alert('请填写收件人！');
      if (!phone) return alert('请填写联系电话！');
      if (province === '请选择省份') return alert('请选择省份！');
      if (city === '请选择城市') return alert('请选择城市！');
      if (county === '请选择地区') return alert('请选择地区！');
      if (!address) return alert('请填写详细收件地址！');
      if (phone.length != 11) return alert('请您填写正确的手机号');
      if(this.payType == 1 && this.onlinePayType == -1) {
        return alert('请选择支付方式！');
      }
			imgStr = arr[this.cardType];

      cardPrice = cardPrice ? +cardPrice : 0;

      if(!cardPrice || cardPrice == 20) {
        if(this.cardType == 'zc') {
          cardPrice = 299;
        }
        else if(this.cardType == 'zt') {
          cardPrice = 2999
        }
        else {
          cardPrice = 199
        }
      } else {
        if(this.cardType == 'zc') {
          cardPrice += 100;
        }
        else if(this.cardType == 'zt') {
          cardPrice += 2800;
        }
      }

			var data = {
        chooseType: this.chooseType,
				cardType: this.cardType,
				cardPrice: cardPrice,
				typeStr: typeStr,
				cardNum: cardNum,
        cardName: (firstName + ' ' + lastName),
				imgStr: imgStr+'?v=20180329',
				name: name,
				phone: phone,
				address: province + ' ' + city + ' ' + county + ' ' + address,
				isMaken: this.isMaken,
				payType: this.payType,
				onlinePayType: this.onlinePayType,
				totalPrice: this.getTotalPrice()
			};
      $('#J_confirm_info').html(template('J_info_tpl', data));
      $('#J_edit').hide();
    	$('#J_confirm').show();
      $('.header').show();
      $(window).scrollTop(0);
      $(window).off('scroll');
      if(this.cardType !== 'zs' || this.cardType !== 'zz' || this.cardType !== 'jd' || this.cardType !== 'zc' || this.cardType !== 'zt') {
        $('.card-name').addClass('card-name-xz');
      }
      else if(
        this.cardType == 'mouse' ||
        this.cardType == 'cattle' ||
        this.cardType == 'tiger' ||
        this.cardType == 'rabbit' ||
        this.cardType == 'dragon' ||
        this.cardType == 'snake' ||
        this.cardType == 'horse' ||
        this.cardType == 'sheep' ||
        this.cardType == 'monkey' ||
        this.cardType == 'rooster' ||
        this.cardType == 'dog' ||
        this.cardType == 'pig'  ||
        this.cardType == 'ns' 
      ) {
        $('.card-name').addClass('card-name-sx');
      }
      else {
        $('.card-name').removeClass('card-name-xz');
      }
    	this.renderCardNumber(cardNum ? (cardNum+''): '101088888');
    },
    getTotalPrice: function() {
      var price = 0;
      if(this.cardType == 'zc') {
          price = 100;
      } else if(this.cardType == 'zt') {
          price = 2800;
      } else {
          price = 0;
      }
      if (this.chooseType > -1) {
          switch (this.chooseType) {
              case 2:
                  price += 199;
                  break;
              case 0:
              case 1:
                  if (this.chooseType == 0) {
                      price += 219
                  } else if (this.chooseType == 1) {
                      price += +$('[name="cardPrice"]').val();
                  }
                  break;
          }
      } else {
          price += 199;
      }
      if(this.isMaken !== null) {
      	if (this.isMaken) {
            price += 100;
        } else {
        }
      }
      return price
    },
    renderBarPrice: function() {
    	var price = this.getTotalPrice();
    	$('#J_bar_total').text('¥' + price);
    },
   	getParams: function() {
    		var cardNum = $('[name="cardNum"]').val();
	      var pinyin = "";
	      var name = $.trim($('[name="name"]').val());
	      var phone = $.trim($('[name="phone"]').val());
	      var address = $.trim($('[name="address"]').val());
	      var province = $('[name="province"] option:selected').text();
	      var city = $('[name="city"] option:selected').text();
	      var county = $('[name="county"] option:selected').text();
	      var firstName = $.trim($('[name="firstName"]').val());
	      var lastName = $.trim($('[name="lastName"]').val());
	      if(firstName && lastName) {
	      	pinyin = firstName + ' ' + lastName;
	      }
	      var params = {
	          name: name,
	          phone: phone,
	          address: address,
	          province: province,
	          city: city,
	          county: county
	      };
	      cardNum && (params['cardNum'] = cardNum);
	      pinyin && (params['pinyin'] = pinyin);
	     	_getUrlParam('pid') && (params['pid'] = _getUrlParam('pid'));
	      if (_getUrlParam('source')) {
          params['adtSource'] = _getUrlParam('source');
	      } else {
	      	params['adtSource'] = 'single';
 	      }

	      if (_getUrlParam('star')) {
	          params['momentStarId'] = _getUrlParam('star');
	      }
	      if (this.cardType == 'znq') {
	          params['cardType'] = 1;
	      } else {
	          params['cardType'] = 0;
	      }
	      if (this.cardType == 'zs') {
	          params['sign'] = 1;
	      }
        else if(this.cardType == 'zc') {
            params['sign'] = 9;
        } 
        else if(this.cardType == 'zt') {
            params['sign'] = 8;
        } 
        else {
	          params['sign'] = 0;
	      }
	      switch(this.cardType) {
	          case 'zz':
	          case 'zs':
	              params['cardStyle'] = 0;
	              break;
	          case 'jd':
	              params['cardStyle'] = 1;
	              break;
	          case 'by':
	              params['cardStyle'] = 2;
	              break;
	          case 'jn':
	              params['cardStyle'] = 3;
	              break;
	          case 'sz':
	              params['cardStyle'] = 4;
	              break;
	          case 'jx':
	              params['cardStyle'] = 5;
	              break;
	          case 'shizi':
	              params['cardStyle'] = 6;
	              break;
	          case 'cn':
	              params['cardStyle'] = 7;
	              break;
	          case 'tc':
	              params['cardStyle'] = 8;
	              break;
	          case 'tx':
	              params['cardStyle'] = 9;
	              break;
	          case 'ss':
	              params['cardStyle'] = 10;
	              break;
	          case 'mj':
	              params['cardStyle'] = 11;
	              break;
	          case 'sp':
	              params['cardStyle'] = 12;
	              break;
	          case 'sy':
	              params['cardStyle'] = 13;
	              break;
            case 'mouse':
                params['cardStyle'] = 19;
                break;
            case 'cattle':
                params['cardStyle'] = 20;
                break;
            case 'tiger':
                params['cardStyle'] = 21;
                break;
            case 'rabbit':
                params['cardStyle'] = 22;
                break;
            case 'dragon':
                params['cardStyle'] = 23;
                break;
            case 'snake':
                params['cardStyle'] = 24;
                break;
            case 'horse':
                params['cardStyle'] = 25;
                break;
            case 'sheep':
                params['cardStyle'] = 26;
                break;
            case 'monkey':
                params['cardStyle'] = 27;
                break;
            case 'rooster':
                params['cardStyle'] = 28;
                break;
            case 'dog':
                params['cardStyle'] = 29;
                break;
             case 'pig':
                params['cardStyle'] = 30;
                break;
            case 'ns':
               params['cardStyle'] = 35;
                break;
	      }
	      try {
	          if(app) {
	              params['source'] = 4;
	          }
	      } catch(e) {
	          console.log(e);
	      }
	      return params
    },
    showQrcode: function(type, orderNum, pid) {
    		var imgHtml = '<img src="' + this.url.qrCode + '?orderNum='+orderNum + '&pid=' + pid + '"/>';
        switch(type) {
            case 'alipay':
            		$('#J_modal_title').html('<img src="https://img1.ultimavip.cn/alipay.png" width="120"/>');
            		imgHtml += '<p class="pd10 fs16">请使用支付宝扫描二维码支付</p>';
                break;
            case 'wechat':
            		$('#J_modal_title').html('<img src="https://img1.ultimavip.cn/WePayLogo.png" width="120"/>');
            		imgHtml += '<p class="pd10 fs16">请使用微信扫描二维码支付</p>';
                break;
        }
        $('#J_modal_content').html(imgHtml);
        $('.overlay').css('height', document.body.clientHeight + 'px');
        $('body').addClass('show-qrcode');
        $('.overlay').off('click').on('click', function() {
        	$('body').removeClass('show-qrcode');
        });
    },
    submit: function(e) {
    	var self = this;
      var params = this.getParams();
      params['eid'] = 1;
      if ($(e.target).hasClass('disabled')) return;
      $(e.target).addClass('disabled');
      try {
          window.BM.fetch({
              type: 1,
              attach: params['phone'],
              page: 5
          })
      } catch (error) {
          console.log(error);
      }
      try {
        _czc.push(["_trackEvent", 'pc购卡页点击（single）', '下一步', '提交订单', 'single']);
      } catch(e) {
        console.log(e); 
      }
      try {
        sensors.track('Search_Select_Payment');
      } catch(e) {
        console.log(e);
      }
      if(this.payType == 1) {
      	$.post(this.url.pay, params, function(response) {
              $(e.target).removeClass('disabled');
              if (response['success']) {
                  var orderNum = response['data'];
                  try {
                      window.BM.fetch({
                          type: 1,
                          page: 6
                      })
                  } catch (error) {
                      console.log(error);
                  }
                  if (self.onlinePayType == 0) {
                        var url = '/alipay.html?orderNum=' + orderNum;
                        if (_getUrlParam('pid')) {
                            url += '&pid=' + _getUrlParam('pid');
                        }
                        window.location.href = url;
                  }
                  if (self.onlinePayType == 1) {
                  		var params = {
                          openid: _getUrlParam('openid'),
                          orderNum: orderNum
                      };
                      if (_getUrlParam('pid')) {
                          params['pid'] = _getUrlParam('pid');
                      }
                      self.showQrcode('wechat', params['orderNum'], params['pid'] || '');
                      self.watchStatus(params['orderNum']);
                  }
                  if (self.onlinePayType == 2) {
                      var params = {
                          orderNum: orderNum
                      };
                      if (_getUrlParam('pid')) {
                          params['pid'] = _getUrlParam('pid');
                      }
                      $.post(self.url.union, params, function(data) {
                          $(e.target).removeClass('disabled');
                          if (data['success']) {
                              var html = data['data'];
                              document.write(html);
                          } else {
                              return alert(data['msg']);
                          }
                      });
                  }
              }
          })
      } else {
      	this.submitInDelivery(params, function() {
      		$(e.target).addClass('disabled');
      	});
      }
    },
    submitInDelivery: function(params, callback) {
    	try {
          window.BM.fetch({
              type: 1,
              page: 7
          })
      } catch (error) {
          console.log(error);
      }
      params['eid'] = 2;
      $.post(this.url.delivery, params, function(response) {
        if (response['success']) {
            window.location.href  = '/success.html';
        } else {
            if (response['inputErrors']) {
                var msg = '';
                for (var i = 0; i < response['inputErrors'].length; i++) {
                    if (i == response['inputErrors'].length - 1) {
                        msg += response['inputErrors'][i]['msg'] + ',';
                    } else {
                        msg += response['inputErrors'][i]['msg'];
                    }
                    alert(msg);
                }
            } else {
                alert(response['msg']);
            }
        }
        callback&&callback();
    	})
    },
    watchStatus: function(orderNum) {
        var self = this;
        this.timer = setTimeout(function() {
            $.get(self.url.status, { orderNum: orderNum }, function(response) {
               if (response['success']) {
                    var status = response['data']['status'];
                    switch (status) {
                        case 1: //买家已付款
                            break;
                        case 2: //交易成功
                            if(!response['data']['openId']) return;
                            $.get(host + '/callback/weixin/success?openId=' + response['data']['openId'],function (res) {
                                  if(res && res.success){
                                      var arr = [];

                                      for(var k in res.data){
                                         arr.push(k,res.data[k]);
                                      }
                                      return (location.href = '/success.html#type=' + arr[0] +'&href=' + arr[1]);
                                  } 
                                  alert(res.msg);
                              });
                            break;
                        case 3: //交易失败
                            alert(response['msg']);
                            break;
                        default:
                            self.watchStatus(orderNum);
                    }
                } else {
                    if (response['code'] == '9993') {
                        alert(response['msg'])
                    }
                }
            });
        }, 2000);
    },
    selectMake: function(e) {
			var type = $(e.target).data('type');
			$(e.target).addClass('m-btn-active').siblings().removeClass('m-btn-active');
			switch(type) {
				case 0: // 不定制
				  this.isMaken = 0;
				  $('#J_make_tips').hide();
      		$('#J_no_make_tips').show();
      		$('#J_make_input').hide();
      		$('#J_bar_make').text('放弃定制');
      		$('#J_bar_make').parent().show();
          $('.J_make_in').val('');
					break;
				case 1: // 定制
				  this.isMaken = 1;
				  $('#J_bar_make').text('姓名定制');
				  $('#J_bar_make').parent().show();
				  $('#J_make_tips').show();
      		$('#J_no_make_tips').hide();
      		$('#J_make_input').show();
					break;
			}
			this.renderBarPrice();
		},
    initPageNum: function() {
			this.pageNum = 1;
		},
    selectSubmitType: function(e) {
    	var type = $(e.target).data('type');
    	$(e.target).addClass('m-btn-active').siblings().removeClass('m-btn-active');
    	switch(type) {
    		case 0: // 货到付款
    		  $('#J_pay').hide();
    		  this.payType = 0;
    			break;
    		case 1: // 在线支付
  				$('#J_pay').show();
  				this.payType = 1;
    			break;
    	}
    },
    selectPayType: function(e) {
    	var type = $(e.currentTarget).data('type');
    	$(e.currentTarget).addClass('m-btn-active').siblings().removeClass('m-btn-active');
    	this.onlinePayType = type;
    },
    switchToEdit: function(e) {
    	var $target = $(e.target);
      var top = $target.closest('.s-step').offset().top;
    	$target.closest('.s-step').removeClass('s-step-complete');
      $target.closest('.s-step').addClass('s-step-switching');
      setTimeout(function() {
        $target.closest('.s-step').removeClass('s-step-switching'); 
        $('html, body').animate({scrollTop: top - 64});
      }, 240)
    },
		bindEvent: function() {
			$('.J_select_card').bind('click', _bind(this.selectCard, this));
			$('.J_select_number').bind('click', _bind(this.selectNumberType, this));
			$('#J_self_switch').bind('click', _bind(this.renderOwnNumber, this));
			$('#J_top_switch').bind('click', _bind(this.renderTopNumber, this));
			$('#J_search_switch').bind('click', _bind(this.switchSearch, this));
			$('#J_search input').bind('change', _bind(this.initPageNum, this));
			$('#J_search_btn').bind('click', _bind(this.search, this));
			$('.J_select_make').bind('click', _bind(this.selectMake, this));

			$('[name="province"]').bind('change', _bind(this.selectProvince, this));
			$('[name="city"]').bind('change', _bind(this.selectCity, this));
			$('[name="county"]').bind('change', function() {
				$('[name="county"]').closest('.select-group').removeClass('select-group-error');
			});

			$('.J_next_btn').bind('click', _bind(this.toNext, this));
			$('.J_prev_btn').bind('click', _bind(this.toPrev, this));

			$('select', '#J_step_2').bind('click', function() {
        $('.form-input', '#J_step_2').each(function() {
          if(!$(this).find('input').val()) {
            $(this).removeClass('form-input-focus');
          }
        })
			});
      $('.form-input', '#J_step_2').bind('click', function() {
        $(this).closest('.form-input').addClass('form-input-focus form-input-active');
        $(this).find('input').focus();
      });
			$('input', '#J_step_2').bind('focus', function() {
				$(this).closest('.form-input').removeClass('form-input-error');
        $(this).closest('.form-input').addClass('form-input-focus form-input-active');
			}).bind('blur', function() {
        if(!this.value) {
          $(this).closest('.form-input').removeClass('form-input-focus');
          $(this).closest('.form-input').removeClass('form-input-active');
        } else {
          $(this).closest('.form-input').removeClass('form-input-focus');
        }
			});
      $('[name="phone"]').bind('blur', function() {
        var phone = this.value;
        if (!/^(13[0-9]|14[579]|15[0-3,5-9]|17[0135678]|18[0-9])\d{8}$/.test(phone)) {
          $(this).closest('.form-input').find('.error-tips').text('请您填写正确的手机号');
          $(this).closest('.form-input').addClass('form-input-error');
          return;
        }
      });
      $('input', '#J_step_4').bind('focus', function() {
        $(this).closest('.form-input').addClass('form-input-focus');
      }).bind('blur', function() {
        $(this).closest('.form-input').removeClass('form-input-focus');
      });
			$('.J_make_in').bind('change', function() {
        if (!/[a-zA-Z]+/g.test(this.value)) {
          this.value = '';
        } else {
          $(this).val(this.value.toUpperCase().replace(/\s/g, ''));
        }
      }).bind('change', function() {
        $(this).closest('.form-input').removeClass('form-input-error');
      });

			$('.J_btn_edit').bind('click', _bind(this.switchToEdit, this));
			$(document).on('click', '.m-number-item',  _bind(this.selectNumber, this));

			$('.J_pay_btn').bind('click', _bind(this.selectSubmitType, this));
			$('.J_btn_pay').bind('click', _bind(this.selectPayType, this));

			$('#J_confirm_btn').bind('click', _bind(this.toConfirm, this));
			$('#J_submit_btn').bind('click', _bind(this.submit, this));
      $(window).on('scroll', _bind(this.scroll, this));
		}
	};

	window.singlePage = function() {
		new _app();
	};

})(window)